<template>
  <!-- 消息配置审核明细-->
  <el-dialog
    :title="title"
    center
    :visible.sync="openState"
    width="800px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" :disabled = "true" label-width="110px">
      <!--消息配置审核明细-->
      <el-card class="form-card TechPara-card">
        <div slot="header" class="label-header-msg">
          <span>消息配置审核明细</span>
        </div>
        <el-row>
          <el-col :span="24">
            <el-form-item label="事件类型" prop="eventType">
              <el-select
                v-model="form.eventNm"
                placeholder="事件类型"
                style="width: 100%"
                :disabled = "true"
              >
                <el-option
                  v-for="dict in eventTypeList"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="isUse">
              <el-select
                v-model="form.isUse"
                placeholder="是否启用"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dictMap.ON_OFF_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="节假日开关" prop="holidayFlag">
              <el-select
                v-model="form.holidayFlag"
                placeholder="节假日开关"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dictMap.HOLIDAYFLAGCODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="发送开始时间" prop="sendStartDt">
              <el-time-picker
                style="width: 100%"
                v-model="form.sendStartDt"
                value-format="HHmmss"
                type="time"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发送结束时间" prop="sendEndDt">
              <el-time-picker
                style="width: 100%"
                v-model="form.sendEndDt"
                value-format="HHmmss"
                type="time"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="通知类型" prop="sendType">
              <el-checkbox-group v-model="sendTypeList">
                <el-checkbox
                  v-for="dict in dictMap.MSG_SEND_TYPE"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.value"
                  @change="sendTypeChange"
                  >{{ dict.text }}
                </el-checkbox
                >
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件接收人小组" prop="mailRecGroup" v-if="mailRecGroupShow">
              <el-select
                multiple
                v-model="mailRecGroupList"
                placeholder="邮件接收人小组"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in msgGroupList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件抄送人小组" prop="mailCopyGroup" v-if="mailCopyGroupShow">
              <el-select
                multiple
                v-model="mailCopyGroupList"
                placeholder="邮件抄送人小组"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in msgGroupList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="短信接收人小组" prop="smsRecGroup" v-if="smsRecGroupShow">
              <el-select
                multiple
                v-model="smsRecGroupList"
                placeholder="短信接收人小组"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in msgGroupList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件主题" prop="mailTopic" v-if="mailTopicShow">
              <el-input
                v-model="form.mailTopic"
                placeholder="邮件主题"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件内容" prop="mailContent" v-if="mailContentShow">
              <el-input
                v-model="form.mailContent"
                placeholder="邮件内容"
                type="textarea"
                autosize 
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="短信内容" prop="smsContent" v-if="smsContentShow">
              <el-input
                v-model="form.smsContent"
                placeholder="短信内容"
                type="textarea"
                autosize 
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="approve" v-show="approveShow">审 核</el-button>
      <el-button type="primary" @click="reject" v-show="rejectShow">拒 绝</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { msgConfApprove,msgConfReject,msgConfApproveDetail,getMonitorConfList,getMsgGroupList} from "@/api/cnaps/bizMng/msgConf.js";
import { listPageMixin } from "@/libs/listPageMixin";

export default {
  name: "msgConfApproveDialog",
  mixins: [listPageMixin],
  components: {
  },
  data() {
    return {
      title: "",
      // 是否显示弹出层
      openState: false,
      mailRecGroupShow: false,
      mailCopyGroupShow: false,
      smsRecGroupShow: false,
      mailTopicShow: false,
      mailContentShow: false,
      smsContentShow: false,
      rejectShow: false,
      approveShow: false,
      sendTypeList: [],
      eventTypeList: [],
      msgGroupList: [],
      mailRecGroupList: [],
      mailCopyGroupList: [],
      smsRecGroupList: [],
      // 字段可编辑控制

      //数据字典
      searchFormDictList: [
        "MSG_SEND_TYPE",
        "ON_OFF_STATUS",
        "HOLIDAYFLAGCODE",
      ],
      // 表单参数
      form:{},
      // 表单校验
      rules: {
        
      },
      opButtonList: () => {
        return [
        ];
      },
    };
  },
  mounted() {
    this.initEventTypeList();
    this.initMsgGroupList();
  },
  methods: {
    // 表单重置
    reset() {
      this.resetForm("form");
      this.$set(this, "form", {});
    },
    // 关闭回掉
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          this.reset();
          done();
        })
        .catch(_ => {});
    },
    show(row) {
      this.form = row;
      this.title = "消息配置审核明细";
      this.openState = true;
      this.msgConfApproveDetail(row);

      if(row.checkStatus == '00'||row.checkStatus == '01'||row.checkStatus == '02'){
        this.approveShow = true;
        this.rejectShow = true;
      }else{
        this.approveShow = false;
        this.rejectShow = false;
      }
    },

    msgConfApproveDetail: function(row) {
      msgConfApproveDetail(row).then(response => {
          this.showData(response);
        })
    },

    showData: function(response) {
      this.form = response.result.data;
      this.initSendType(this.form);
      this.initMailRecGroup(this.form);
      this.initMailCopyGroup(this.form);
      this.initSmsRecGroup(this.form);
      this.sendTypeChange();
    },
    /** 初始化通知类型 */
    initSendType(row) {
      if(typeof row.sendType !== 'undefined'){
        this.sendTypeList = row.sendType.split(",");
      }
    },  
    /** 初始化邮件接收小组名称 */
    initMailRecGroup(row) {
      if(typeof row.mailRecGroup !== 'undefined'){
        this.mailRecGroupList = row.mailRecGroup.split(",");
      }
    }, 

    /** 初始化邮件抄送小组名称 */
    initMailCopyGroup(row) {
      if(typeof row.mailCopyGroup !== 'undefined'){
        this.mailCopyGroupList = row.mailCopyGroup.split(",");
      }
    }, 

    /** 初始化短信接收小组名称 */
    initSmsRecGroup(row) {
      if(typeof row.smsRecGroup !== 'undefined'){
        this.smsRecGroupList = row.smsRecGroup.split(",");
      }
    }, 

    /** TODO审批按钮 */
    approve() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          msgConfApprove(this.form).then(res => {
            if (res.code === 'S') {
              this.msgSuccess("审批通过");
              this.cancel();
            }
          });
        }
      });
    },

    /** TODO拒绝按钮 */
    reject() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          msgConfReject(this.form).then(res => {
            if (res.code === 'S') {
              this.msgSuccess("审批已拒绝");
              this.cancel();
            }
          });
        }
      });
    },

    cancel() {
      this.openState = false;
      this.reset();
      this.$emit("editComplete", true);
    },

    //初始化获取监控事件列表
    initEventTypeList(){
      getMonitorConfList(this.form).then(res => {
        if (res.code === 'S') {
          const arr = [];
          const list = (res.result) || [];
          list.length &&list.forEach((v) => {
            arr.push({
              value: v.monitorId,
              text: v.eventNm,
            });
          });
          this.eventTypeList = arr;
        }
      });
    },

    //初始化获取消息分组
    initMsgGroupList(){
      getMsgGroupList(this.form).then(res => {
        if (res.code === 'S') {
          const arr = [];
          const list = (res.result) || [];
          list.length &&list.forEach((v) => {
            arr.push({
              value: v.groupId,
              text: v.groupNm,
            });
          });
          this.msgGroupList = arr;
        }
      });
    },

    sendTypeChange() {
      const sendType = this.sendTypeList;

      if(sendType.indexOf("01")>-1&sendType.indexOf("02")>-1){//邮件通知和短信通知
        this.mailRecGroupShow = true;
        this.mailCopyGroupShow = true;
        this.mailTopicShow = true;
        this.mailContentShow = true;
        this.smsRecGroupShow = true;
        this.smsContentShow = true;

        this.rules.mailRecGroup= {
          required: true,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        }
        this.rules.mailCopyGroup= {
          required: true,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        }
        this.rules.mailTopic= {
          required: true,
          message: "请输入邮件主题",
          trigger: "blur",
        }
        this.rules.mailContent= {
          required: true,
          message: "请输入邮件内容",
          trigger: "blur",
        }
        this.rules.smsRecGroup= {
          required: true,
          message: "请选择短信接收人小组",
          trigger: "blur",
        }
        this.rules.smsContent= {
          required: true,
          message: "请输入短信内容",
          trigger: "blur",
        }
      }else if(sendType.indexOf("01")>-1&sendType.indexOf("02")<0){//邮件通知
        this.mailRecGroupShow = true;
        this.mailCopyGroupShow = true;
        this.mailTopicShow = true;
        this.mailContentShow = true;
        this.smsRecGroupShow = false;
        this.smsContentShow = false;

        this.rules.mailRecGroup= {
          required: true,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        }
        this.rules.mailCopyGroup= {
          required: true,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        }
        this.rules.mailTopic= {
          required: true,
          message: "请输入邮件主题",
          trigger: "blur",
        }
        this.rules.mailContent= {
          required: true,
          message: "请输入邮件内容",
          trigger: "blur",
        }
        this.rules.smsRecGroup= {
          required: false,
          message: "请选择短信接收人小组",
          trigger: "blur",
        }
        this.rules.smsContent= {
          required: false,
          message: "请输入短信内容",
          trigger: "blur",
        }
      }else if(sendType.indexOf("02")>-1&sendType.indexOf("01")<0){//短信通知
        this.mailRecGroupShow = false;
        this.mailCopyGroupShow = false;
        this.mailTopicShow = false;
        this.mailContentShow = false;
        this.smsRecGroupShow = true;
        this.smsContentShow = true;

        this.rules.mailRecGroup= {
          required: false,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        }
        this.rules.mailCopyGroup= {
          required: false,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        }
        this.rules.mailTopic= {
          required: false,
          message: "请输入邮件主题",
          trigger: "blur",
        }
        this.rules.mailContent= {
          required: false,
          message: "请输入邮件内容",
          trigger: "blur",
        }
        this.rules.smsRecGroup= {
          required: true,
          message: "请选择短信接收人小组",
          trigger: "blur",
        }
        this.rules.smsContent= {
          required: true,
          message: "请输入短信内容",
          trigger: "blur",
        }
      }else{//都不包含
        this.mailRecGroupShow = false;
        this.mailCopyGroupShow = false;
        this.mailTopicShow = false;
        this.mailContentShow = false;
        this.smsRecGroupShow = false;
        this.smsContentShow = false;

        this.rules.mailRecGroup= {
          required: false,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        }
        this.rules.mailCopyGroup= {
          required: false,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        }
        this.rules.mailTopic= {
          required: false,
          message: "请输入邮件主题",
          trigger: "blur",
        }
        this.rules.mailContent= {
          required: false,
          message: "请输入邮件内容",
          trigger: "blur",
        }
        this.rules.smsRecGroup= {
          required: false,
          message: "请选择短信接收人小组",
          trigger: "blur",
        }
        this.rules.smsContent= {
          required: false,
          message: "请输入短信内容",
          trigger: "blur",
        }
      }
    }
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
